package me.lc.jokealot.util;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonHelper {
	private static Gson gson = new Gson();

	public static String getJsonString(Object obj) {
		try {
			return gson.toJson(obj);
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
	}

	public static String getJsonString(Object obj, Type typeofSrc) {
		try {
			return gson.toJson(obj, typeofSrc);
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
	}

	public static Object getObjectbyJsonString(String jsonStr, TypeToken tt) {
		try {
			return gson.fromJson(jsonStr, tt.getType());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Object getObjectbyJsonString(String jsonStr, Type srcType) {
		try {
			return gson.fromJson(jsonStr, srcType);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
